package cn.tedu.charging.order.service.impl;

import cn.tedu.charging.commen.JsonResult;
import cn.tedu.charging.commen.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.commen.pojo.vo.StationVO;
import cn.tedu.charging.commen.pojo.vo.UserInfoVO;
import cn.tedu.charging.commen.utils.JsonUtils;
import cn.tedu.charging.order.common.ChargingConstants;
import cn.tedu.charging.order.common.GunStatusConstants;
import cn.tedu.charging.order.common.MqttConstants;
import cn.tedu.charging.order.feign.DeviceClient;
import cn.tedu.charging.order.feign.UserClient;
import cn.tedu.charging.order.mqtt.MqttProducer;
import cn.tedu.charging.order.pojo.dto.ChargingDto;
import cn.tedu.charging.order.pojo.dto.OrderMQDto;
import cn.tedu.charging.order.pojo.param.OrderAddParam;
import cn.tedu.charging.order.quartz.DeviceQuartzJob;
import cn.tedu.charging.order.rabbitmq.RabbitMQProducer;
import cn.tedu.charging.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Override
    public String createOrder(OrderAddParam orderAddParam) {
        log.debug("创建订单业务，参数={}",orderAddParam);

        String orderNo = getOrderNo();
        log.debug("获取订单号，入参:{},出参:{}",orderAddParam,orderNo);

        log.debug("获取场站信息，参数={}",orderAddParam.getGunId());
        StationVO stationVO = getStationInfo(orderAddParam.getGunId());
        log.debug("获取场站信息，入参:{},出参:{}",orderAddParam.getGunId(),stationVO);

        log.debug("更新枪机状态，参数={}",orderAddParam.getGunId());
        Boolean gunStatus = updateGunStatus(orderAddParam.getGunId());
        log.debug("更新枪机状态，入参:{},出参:{}",orderAddParam.getGunId(),gunStatus);

        log.debug("获取用户信息，参数={}",orderAddParam.getUserId());
        UserInfoVO userInfoVO = getUserInfo(orderAddParam.getUserId());
        log.debug("获取用户信息，入参:{},出参:{}",orderAddParam.getUserId(),userInfoVO);

        //向设备发送开始充电消息
        startCharging(orderNo, orderAddParam.getPileId(), orderAddParam.getGunId());
        //向消息队列发送消息
        Boolean b = sendMessageToRabbit(orderNo);

        //创建定时任务
        try {
            DeviceQuartzJob deviceQuartzJob = new DeviceQuartzJob(orderNo, orderAddParam.getGunId());
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
        log.debug("创建订单业务，入参:{}，出参:{}",orderAddParam,orderNo);
        return orderNo;
    }

    @Autowired
    private RabbitMQProducer rabbitMQProducer;

    public Boolean sendMessageToRabbit(String orderNo){
        OrderMQDto orderMQDto = new OrderMQDto();
        orderMQDto.setOrderNo(orderNo);
        rabbitMQProducer.send(orderMQDto);
        return true;
    }

    @Autowired
    private DeviceClient deviceClient;

    private StationVO getStationInfo(Integer gunId){
        JsonResult<StationVO> stationInfo = deviceClient.getStationInfo(gunId);
        if (stationInfo != null){
            return stationInfo.getData();
        }
        return null;
    }

    private Boolean updateGunStatus(Integer gunId){
        GunStatusUpdateParam gunStatusUpdateParam = new GunStatusUpdateParam();
        gunStatusUpdateParam.setGunId(gunId);
        gunStatusUpdateParam.setStatus(GunStatusConstants.GUN_STATUS_BUSY);
        JsonResult<Boolean> booleanJsonResult = deviceClient.updateGunStatus(gunStatusUpdateParam);
        return booleanJsonResult.getData();
    }

    @Autowired
    private UserClient userClient;

    private UserInfoVO getUserInfo(Integer userId){
        JsonResult<List<UserInfoVO>> userInfoListJsonResult = userClient.getUserInfo(userId);
        if (userInfoListJsonResult != null){
            return userInfoListJsonResult.getData().get(0);
        }
        return null;
    }

    private String getOrderNo(){
        return "666";
    }

    @Autowired
    private MqttProducer mqttProducer;

    public void startCharging(String orderNo,Integer pileId,Integer gunId){
        String topic = MqttConstants.TOPIC_START_CHARGING_PREFIX + pileId;
        ChargingDto chargingDto = new ChargingDto();
        chargingDto.setOrderNo(orderNo);
        chargingDto.setPileId(pileId);
        chargingDto.setGunId(gunId);
        chargingDto.setMsg(ChargingConstants.START_CHARGING);
        String message = JsonUtils.toJson(chargingDto);
        log.debug("发送消息，主题:{},消息:{}",topic,message);
        Boolean success = mqttProducer.send(topic,message);
        log.debug("发送消息，主题:{},消息:{},结果:{}",topic,message,success);
    }
}
